package com.gab.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gab.mapper.TbCompanyMapper;
import com.gab.mapper.TbImageMapper;
import com.gab.pojo.TbCompany;
import com.gab.service.TbCompanyService;
import com.gab.service.TbImageService;
import com.gab.utils.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import  com.gab.pojo.TbImageExample;
import  com.gab.pojo.TbImageExample.Criteria;
import com.gab.pojo.TbImage;

@Service
public class TbImageServiceImpl  implements TbImageService{
	@Autowired
	private TbImageMapper tbImageMapper;
	
	public List<TbImage> findAll() {
		// TODO Auto-generated method stub
		return tbImageMapper.selectByExample(null);
	}

	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbImage> page=   (Page<TbImage>) tbImageMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	public void add(TbImage tbImage) {
		tbImageMapper.insert(tbImage);
		
	}

	public void update(TbImage tbImage) {
		tbImageMapper.updateByPrimaryKey(tbImage);
		
	}

	public TbImage findOne(Long id) {
		
		return tbImageMapper.selectByPrimaryKey(id);
	}
//	banner的查询ID
	public TbImage findOneban(Long id) {
		return tbImageMapper.selectByPrimaryKeyBan(id);
	}
	
	public void delete(Long[] ids) {
		for(Long id:ids){
			tbImageMapper.deleteByPrimaryKey(id);
		}
		
	}

	public PageResult findPage(TbImage tbImage, int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
		
		TbImageExample example=new TbImageExample();
		Criteria criteria = example.createCriteria();
		
		if(tbImage!=null){			
			if(tbImage.getDetail()!=null && tbImage.getDetail().length()>0){
				criteria.andDetailLike("%"+tbImage.getDetail()+"%");
			}
			if(tbImage.getType()!=null ){
				criteria.andTypeEqualTo(tbImage.getType());
			}
			if(tbImage.getUrl()!=null && tbImage.getUrl().length()>0){
				criteria.andUrlLike("%"+tbImage.getUrl()+"%");
			}
	
		}
		
		Page<TbImage> page= (Page<TbImage>)tbImageMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}

}
